<template>
 <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">用户注册</div>
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px" class="ms-content">
               <el-form-item prop="username" label="用户名">
                    <el-input v-model="ruleForm.username" >
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password" label="密码">
                    <el-input type="password"  v-model="ruleForm.password" >
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>
                 <el-form-item prop="email" label="邮箱">
                    <el-input type="email"  v-model="ruleForm.email" >
                         <el-button slot="prepend" icon="el-icon-lx-people"></el-button>    
                         <template slot="append">.com</template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="vertify" label="验证码">
                    <el-input  v-model="ruleForm.vertify" >
                         <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                         <template slot="append"><img @click="getVertify()" :src="src" alt="" style="width:30px;height:15px;"></template>
                    </el-input>
                </el-form-item>
                 <div class="login-btn">
                     <el-button type="primary" @click="submitForm('ruleForm')" >注册</el-button>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
    import qs from 'qs'
    import {getSecurity} from '../../api/api'
    import {register} from '../../api/api'
    import {isusernameexist} from '../../api/api'
    //定义一个全局的变量，谁用谁知道
    var validUsername=(rule, value,callback)=>{
        isusernameexist({'username':value}).then(data=>{
            if(data.code == 1){
                 callback(new Error('用户名已存在'))
            }else{
                 callback()
            }
        });
    }
    export default {
        data: function(){
            return {
                ruleForm: {
                    username: '',
                    password: '',
                    email:'',
                    vertify:''
                },
                rules: {
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                        {validator:validUsername}
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' }
                    ],
                     email: [
                        { required: true, message: '请输入邮箱', trigger: 'blur' }
                    ],
                    vertify: [
                        { required: true, message: '请输入验证码', trigger: 'blur' }
                    ]
                },
                vertify:'',
                src:''
            }
        },
        mounted(){
            this.getVertify();
        },
        methods: {
            getVertify(){
                getSecurity().then(data=>{
                    this.src = ''
                    this.vertify = data.verification;
                    console.log(data.verification);
                    this.src = 'http://192.168.1.176:8081/static/images/verification.jpg?'+Math.random();
                });
            },
             submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if(this.ruleForm.vertify != this.vertify){
                            this.$message({
                                message:'验证码错误',
                                type: 'error'
                            });
                            this.getVertify();
                            this.ruleForm.vertify = '';
                        }else{
                            this.ruleForm.email = this.ruleForm.email+'.com';
                            register(qs.stringify(this.ruleForm)).then(data=>{
                                if(data.code == 1){
                                    this.$router.push('/');
                                    this.$message({
                                        message:'注册成功',
                                        type: 'success'
                                    });
                                }
                            })
                        }
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            }
        }
    }
</script>

 <style lang="scss" scoped>
.login-wrap{
        position: relative;
        width:100%;
        height:100%;
        background-image: url(../../assets/login-bg.jpg);
        background-size: 100%;
    }
    .ms-title{
        width:100%;
        line-height: 50px;
        text-align: center;
        font-size:20px;
        border-bottom: 1px solid #ddd;
    }
    .ms-login{
        position: absolute;
        left:50%;
        top:50%;
        width:400px;
        margin:-190px 0 0 -175px;
        border-radius: 5px;
        background: rgba(255,255,255, 0.3);
        overflow: hidden;
    }
    .ms-content{
        padding: 30px 30px;
    }
    .login-btn{
        text-align: center;
    }
    .login-btn button{
        width:100%;
        height:36px;
        margin-bottom: 10px;
    }
    .login-tips{
        font-size:12px;
        line-height:30px;
        color:#fff;
    }
    .el-form-item__label{
        color: #000;
    }
</style>
